import { POST_MSG_TYPE } from "@/utils/enums";
import { onRecieveConclusionUrl } from "@/utils/bilibili/index";

export default defineBackground(() => {

  // 需要抓取的接口
  let crawledUrlList = ['https://api.bilibili.com/x/web-interface/view/conclusion/get']

  chrome.sidePanel
    .setPanelBehavior({ openPanelOnActionClick: true })
    .catch((error) => console.error(error));

  chrome.webRequest.onBeforeRequest.addListener(
    function (details) {
      const matched = crawledUrlList.some((url) => details.url.startsWith(url))
      if (matched) {
        onRecieveConclusionUrl(details.url).then(() => {
          chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
            // 然后再使用tabId进行向sidepanel发送消息等操作
            chrome.runtime.sendMessage({
              type: POST_MSG_TYPE.conclusion,
            });
          });
        })
      }

    },
    { urls: ["<all_urls>"] },
    ["requestBody"]
  );
});


